package com.mz.ozy.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.bean.QiNiuBean;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by administrator on 2019/4/8.
 */
public class EvaluatePhotoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    /**
     * 点击添加图片跳转
     */
    private ReleaseMsgAdapter.onAddPicClickListener mOnAddPicClickListener;
    private int mSelectMax;
    private int addimage;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data     A new list is created out of this one to avoid mutable list
     * @param addphoto
     */
    public EvaluatePhotoAdapter(List<String> data, int addphoto) {
        super(R.layout.item_post_msg_pic, data);
        addimage = addphoto;
    }

    @Override
    protected void convert(final BaseViewHolder helper, String item) {
        ImageView ivPostPic = helper.getView(R.id.iv_post_msg_pic);
        ImageView ivPostDel = helper.getView(R.id.iv_post_msg_del);
        if (helper.getItemViewType() == TYPE_CAMERA) {
            ivPostPic.setImageResource(addimage);

            ivPostPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnAddPicClickListener != null) {
                        mOnAddPicClickListener.onAddPicClick();
                    }
                }
            });

            ivPostDel.setVisibility(View.INVISIBLE);
        } else {
            ivPostDel.setVisibility(View.VISIBLE);
            ivPostDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = helper.getAdapterPosition();
                    // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                    // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                    if (index != RecyclerView.NO_POSITION) {
                        getData().remove(index);
                        notifyItemRemoved(index);
                        notifyItemRangeChanged(index, getData().size());
                        if (mOnAddPicClickListener != null) {
                            mOnAddPicClickListener.onRemovePicClick(index);
                        }
                    }
                }
            });

            GlideUtils.loadImg(mContext, ivPostPic, item, R.mipmap.pic_default_appeal);
        }
    }

    @Override
    public int getItemCount() {
        if (getData().size() < mSelectMax) {
            return getData().size() + 1;
        } else {
            return getData().size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    private boolean isShowAddItem(int position) {
        int size = getData().size();
        return position == size;
    }

    public void addMoreItem(List<String> list) {
        addData(list);
    }

    public void addNewItem(String path) {
        addData(path);
    }

    public void setOnAddPicClickListener(ReleaseMsgAdapter.onAddPicClickListener onAddPicClickListener) {
        mOnAddPicClickListener = onAddPicClickListener;
    }

    public void setSelectMax(int selectMax) {
        mSelectMax = selectMax;
    }

    public interface onAddPicClickListener {
        void onAddPicClick();

        void onRemovePicClick(int index);
    }
}
